<?php
/**
 * 《银行帐号》实现类
 *
 * @author 孙晓晔
 * @version $Id: Work.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/FinanceUtil.class.php');

class Bank extends FinanceUtil {
	public $tbl = Constant::tbl_cw_bank;
	
	public $bank = array(
		'-' => '请选择',			
		'招商银行' => '招商银行',
		'中信银行' => '中信银行',
		'汇丰银行' => '汇丰银行',
			
		'中国银行' => '中国银行',
		'中国工商银行' => '中国工商银行',
		'中国建设银行' => '中国建设银行',
		'中国农业银行' => '中国农业银行',	
		'中国民生银行' => '中国民生银行',
		'深圳农村商业银行' => '深圳农村商业银行',
		'东莞农村商业银行' => '东莞农村商业银行',
		'中国邮政储蓄银行' => '中国邮政储蓄银行',
		'上海浦东发展银行' => '上海浦东发展银行',
		
		'东莞银行' => '东莞银行',
		'交通银行' => '交通银行',
		'平安银行' => '平安银行',
		'华夏银行' => '华夏银行',
		
		'广发银行' => '广发银行',
		'兴业银行' => '兴业银行',
		'渣打银行' => '渣打银行',
	);
	
	public $tec = array(
		'-' => '请选择',		
		'1' => '对私工资卡',
		'2' => '对公工资卡',
		'3' => '工资卡',

		'9' => '其它',
	);
	
	public $category = array(
		'-' => '请选择',
		'1' => '境内银行',
		'2' => '境外银行',

		'9' => '其它',
	);
	
	public $info = array(
		'-' => '请选择',
		'1' => '员工',
		'2' => '供应商',
		'3' => '客户',

		'9' => '其它',
	);
	
	public $type = array(
		'-' => '请选择',
		'1' => '个人',
		'2' => '单位',

		'9' => '其它',
	);
	
	public $status = array(
		'-' => '请选择',
		'1' => '正常',
		'2' => '暂停',
		'3' => '停止',

		'9' => '其它',
	);
	
	public $orderby = array(
		'-' => '请选择',
		'info' => '分类',
		'info_id' => '数据',
	
		'bank' => '开户行',
		'branch' => '分支行',
		'name' => '户名',
		'code' => '帐号',
		
		'orderby' => '排序',
	
		'1' => '创建时间',
		'2' => '更新时间',
	);
	
	public $groupby = array(
		'-' => '请选择',
		'oper' => '录入员',
		'info' => '分类',
		'status' => '状态',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
		
		$controller->assign_by_ref('bank_option', $this->bank);
		$controller->assign_by_ref('category_option', $this->category);

		$controller->assign_by_ref('info_option', $this->info);
		$controller->assign_by_ref('type_option', $this->type);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->assign('status_s', '1');
		
		$controller->display($request, 'finance/bank/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}
	
		$emp = $_SESSION['id'];
	
		$p = array('1'=>'12', '2'=>'13', '3'=>'14');
	
		$a = $this->_audit_office($p);
		if (count($a) > 0) {
			$office = implode(",", $a);
	
			return sprintf(" visible = 1 AND (office in (%s) OR oper = '%s')", $office, $emp);
		}
	
		return sprintf(" visible = 1 AND (emp = '%s' OR oper = '%s')", $emp, $emp);
	}

	function _where($request) {		
		$info_s = $request['info_s'];
		$info_id_s = $this->_input($request['info_id_s']);
		
		$category_s = $request['category_s'];
		
		$bank_s = $request['bank_s'];
		$name_s = $request['name_s'];
		$code_s = $request['code_s'];
		
		$type_s = $request['type_s'];
		
		$where = " WHERE 1=1";
		
		if (!empty($info_s) && $info_s <> '-') {
			$where .= " AND info = '" . $info_s . "'";
		}
		
		if (!empty($info_id_s)) {
			$where .= " AND a.info_id = '" . $info_id_s . "'";
		}
		
		if (!empty($category_s) && $category_s <> '-') {
			$where .= " AND category = '" . $category_s . "'";
		}

		if (!empty($bank_s)) {
			$where .= " AND bank LIKE '" . $bank_s . "%'";
		}
		
		if (!empty($name_s)) {
			$where .= " AND name LIKE '%" . $name_s . "%'";
		}
		
		if (!empty($code_s)) {
			$where .= " AND code LIKE '%" . $code_s . "%'";
		}

		if (!empty($type_s) && $type_s <> '-') {
			$where .= " AND type = '" . $type_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		// $fmt = "SELECT a.* FROM %s a, %s b %s %s";
		$fmt = "SELECT a.* FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		// $sql = sprintf($fmt, $this->tbl, Constant::tbl_emp, $where, $orderby);
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);
								
				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}
		
		if ($request['page'] == 'next') {
			$controller->display($request, 'finance/bank/next');
		} else {
			$controller->display($request, 'finance/bank/list');
		}

		return true;
	}
	
	function _info_id_t($info, $id) {
		if ($info == '1') {
			return Name::emp($id);
		} elseif ($info == '2') {
			return $this->_supplier_name($id);
		}
		
		return $this->_customer_code($id);
	}
	
	function _supplier_name($id) {
		if (empty($id)) {
			return '';
		}

		return Common::name_id(Constant::tbl_supplier, $id);
	}
	
	function _customer_code($id) {
		if (empty($id)) {
			return '';
		}
	
		return Common::code_id(Constant::tbl_customer, $id);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'finance/bank/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$info = $request['info'];
		$info_id = $this->_input($request['info_id']);
		
		$category = $request['category'];
		$bank = $request['bank'];
		$branch = $request['branch'];
		$bankcode = $this->_input($request['bankcode']);
		
		$province = $request['province'];
		$city = $request['city'];
		
		$type = $request['type'];
		$zone = $request['zone'];
		
		$swift = trim($request['swift']);
		
		$name = trim($request['name']);
		$zjm = getPinyin($name);
		$zjm = strtoupper($zjm);
			
		$code = trim($request['code']);
		$code = str_replace(" ", "", $code);
		
		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(info, info_id, category, bank, branch, bankcode, swift, province, city, zone, type, name, zjm, code, content, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $info, $info_id, $category, $bank, $branch, $bankcode, $swift, $province, $city, $zone, $type, $name, $zjm, $code, $content, $_SESSION['id']);
		debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/bank/copy');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);
		
		$controller->assign_by_ref('sequence_option', range(0, 12, 1));

		$controller->display($request, 'finance/bank/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$info = $request['info'];
		$info_id = $this->_input($request['info_id']);
		
		$category = $request['category'];
		$bank = $request['bank'];
		$branch = $request['branch'];
		$bankcode = $this->_input($request['bankcode']);
		
		$province = $request['province'];
		$city = $request['city'];
		
		$type = $request['type'];
		$zone = $request['zone'];
		
		$swift = trim($request['swift']);
		
		$name = trim($request['name']);
		$zjm = getPinyin($name);
		$zjm = strtoupper($zjm);
			
		$code = trim($request['code']);
		$code = str_replace(" ", "", $code);
		
		$content = addslashes($request['content']);
		$orderby = $request['orderby'];
		$status = $request['status'];
		
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET info = '%s', info_id = '%s', category = '%s', bank = '%s', branch = '%s', bankcode = '%s', swift = '%s', province = '%s', city = '%s', type = '%s', zone = '%s'";
		$fmt .= ", name = '%s', zjm = '%s', code = '%s', content = '%s', orderby = '%s', status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $info, $info_id, $category, $bank, $branch, $bankcode, $swift, $province, $city, $type, $zone, $name, $zjm, $code, $content, $orderby, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/bank/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['category_t'] = $this->category[$row['category']];
		
		$row['tec_t'] = $this->tec[$row['tec']];
						
		$row['info_t'] = $this->info[$row['info']];				
		$row['info_id_t'] = $this->_info_id_t($row['info'], $row['info_id']);
		
		$row['type_t'] = $this->type[$row['type']];
		$row['bankcode_t'] = $this->_bankcode($row['bankcode']);		
		
		$row = $this->_view_t($row);

		return $row;
	}
	
	function _bankcode($code) {
		require_once(dirname(__FILE__) . '/../info/BankCode.class.php');
		$aa = new BankCode();
				
		return $aa->_code($code);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$insql = "in ('000000000";
		while (list($k, $v) = each($request)) {
			if (substr($k, 0, 2) == "c_") {
				$insql .= "', '" . $v;
			}
		}
		$insql .= "')";
		$sql = "DELETE FROM " . $this->tbl . " WHERE id " . $insql;
		// debug($sql);
		$query = $db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function tj($controller, $request) {
		$bb = $this->_list_email('305');
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			// echo "222";
			return false;
		}

		$from = $this->email;
		$subject = '银行帐号';
		
		$cc = array(	
			'新增信息'=>array('w' => "DATE_FORMAT(createtime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d')", 'b'=>'info, status'),
			'更新信息'=>array('w' => "DATE_FORMAT(modifytime, '%Y-%m-%d') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 DAY), '%Y-%m-%d') AND DATE_FORMAT(createtime, '%Y-%m-%d') <> DATE_FORMAT(modifytime, '%Y-%m-%d')", 'b'=>'info, status'),			
		);
		
		if ($this->_tj_cc($controller, $cc)) {
			echo "222";
			return;
		}

		$controller->clear_assign('result');
		
		$body = $this->_tj_html($controller, $request, 'finance/bank/tj_list');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
	
	function tjw($controller, $request) {
		$bb = $this->_list_email('305');
		// print_r($bb);
		if (!$bb || count($bb) == 0) {
			return false;
		}
		
		$this->pretreat($controller);

		$from = $this->email;
		$subject = '银行帐号（周报）';
		
		$cc = array(	
			'新增信息'=>array('w' => "DATE_FORMAT(createtime, '%Y-%V') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 WEEK), '%Y-%V')", 'b'=>'info, status'),
			'更新信息'=>array('w' => "DATE_FORMAT(modifytime, '%Y-%V') = DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 WEEK), '%Y-%V') AND DATE_FORMAT(createtime, '%Y-%m-%d') <> DATE_FORMAT(modifytime, '%Y-%m-%d')", 'b'=>'info, status'),			
		);
		
		$this->_tj_cc($controller, $cc);
		
		$aa = array(
			'一个星期之内'=>array('w' => "WHERE modifytime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -7 DAY), '%Y-%m-%d')"),
			'一个月之内'=>array('w' => "WHERE modifytime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 MONTH), '%Y-%m-%d')"),
			'三个月之内'=>array('w' => "WHERE modifytime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -3 MONTH), '%Y-%m-%d')"),
			'半年之内'=>array('w' => "WHERE modifytime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -6 MONTH), '%Y-%m-%d')"),
			'一年之内'=>array('w' => "WHERE modifytime >= DATE_FORMAT(DATE_ADD(now(), INTERVAL -1 YEAR), '%Y-%m-%d')"),
			'全部'=>array('w' => "")
		);
		
		$this->_tj_bb($controller, $aa);

		$body = $this->_tj_html($controller, $request, 'finance/bank/tj_list');
		
		$this->_tj_mail($from, $bb, $subject, $body);

		logdate($controller, $request);
		return true;
	}
		
	function _tj2($v) {
		$this->result_s = 10;
		$this->groupby_s = 'oper';
			
		$s = $this->groupby_s;

		if (!empty($v['w'])) {
			$fmt = "SELECT %s AS a, COUNT(id) AS c FROM %s a %s GROUP BY %s ORDER BY c DESC";
			$sql = sprintf($fmt, $s, $this->tbl, $v['w'], $s);
		} else {
			$fmt = "SELECT %s AS a, COUNT(id) AS c FROM %s a GROUP BY %s ORDER BY c DESC";
			$sql = sprintf($fmt, $s, $this->tbl, $s);
		}		
		echo $sql . "\n";

		return $this->_group_tj2($sql);
	}
	
	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'oper');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}
	
	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'info') {
			$row['a'] = $this->info[$row['a']];
		} else {
		
			$row = $this->_name_t($row, $px);
		}

		return $row;
	}
	
	function num($info, $id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT COUNT(*) t FROM %s WHERE info = '%s' AND info_id = '%s' AND status = '1'";
		$sql = sprintf($fmt, $this->tbl, $info, $id);
		// debug($sql);
		$result = $db->query($sql);
		$row = $db->fetch_array($result);

		return $row['t'];
	}
	
	function t($controller, $request) {
		$db = newdb($this->dsn_w);

		$file = "/home/sunxy/n/bank.txt";
		$fd = fopen($file, "r");
		while (!feof($fd)) {
			$buf = trim(fgets($fd));
			// echo $buf;

			if (strlen($buf) < 5 || preg_match("/^#/i", $buf)) {
				continue;
			}
			
			$a = preg_split("/;/", $buf);

			// print_r($a);

			$name = trim($a[0]);
			$province = trim($a[1]);
			$city = trim($a[2]);
			$branch = trim($a[3]);
			$zone = trim($a[4]);
			$branch_name = trim($a[5]);
			$code = trim($a[6]);
			$type = trim($a[7]);
			$content = trim($a[8]);

			$fmt = "INSERT INTO %s(name, province, city, branch, zone, branch_name, code, type, content, createtime, modifytime)";
			$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
			$sql = sprintf($fmt, $this->tbl, $name, $province, $city, $branch, $zone, $branch_name, $code, $type, $content);
			echo $sql . "\n";
			$db->query($sql);
		}
		fclose($fd);

		echo "\n" . $_REQUEST['do'] . "\n";

		logdate($controller, $request);
		return true;
	}
	
	function gh($controller, $request) {
		$db = newdb($this->dsn_w);

		$file = "/home/sunxy/gh3.csv";
		$fd = fopen($file, "r");
		while (!feof($fd)) {
			$buf = trim(fgets($fd));
			// echo $buf;

			if (strlen($buf) < 5 || preg_match("/^#/i", $buf)) {
				continue;
			}
			
			$a = preg_split("/;/", $buf);

			// print_r($a);
						
			$bank = '中国工商银行';
			$branch = '深圳新沙支行';
			
			$province = '广东';
			$city = '深圳';			
			$zone = '0755';
			$type = '1';
			
			$name = trim($a[0]);			
			$code = trim($a[1]);
			$code = str_replace(" ", "", $code);
			
			$info_id = $this->_emp_name($name);
			
			$zjm = getPinyin($name);
			$zjm = strtoupper($zjm);
			
			$fmt = "SELECT * FROM %s WHERE code = '%s'";
			$sql = sprintf($fmt, $this->tbl, $code);
			echo $sql . "\n";
			$result = $db->query($sql);
			if ($db->num_rows($result) > 0) {
				echo "S";
				continue;
			}

			$fmt = "INSERT INTO %s(info, info_id, tec, bank, branch, province, city, zone, type, name, zjm, code, createtime, modifytime)";
			$fmt .= " VALUES('1', '%s', '2', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
			$sql = sprintf($fmt, $this->tbl, $info_id, $bank, $branch, $province, $city, $zone, $type, $name, $zjm, $code);
			echo $sql . "\n";
			$db->query($sql);
		}
		fclose($fd);

		echo "\n" . $_REQUEST['do'] . "\n";

		logdate($controller, $request);
		return true;
	}
	
	function _emp_name($name) {
		$row = Common::name(Constant::tbl_emp, $name);
		
		return $row['id'];
	}
	
	function zh($controller, $request) {
		$db = newdb($this->dsn_w);

		$file = "/home/sunxy/n/zh.txt";
		$fd = fopen($file, "r");
		while (!feof($fd)) {
			$buf = trim(fgets($fd));
			// echo $buf;

			if (strlen($buf) < 5 || preg_match("/^#/i", $buf)) {
				continue;
			}
			
			$a = preg_split("/;/", $buf);

			// print_r($a);

			$bank = '招商银行';
			$branch = '深圳分行';
			
			$province = '广东';
			$city = '深圳';			
			$zone = '0755';
			$type = '1';
			
			$name = trim($a[0]);			
			$code = trim($a[1]);
			
			$info_id = $this->_emp_name($name);
			
			$zjm = getPinyin($name);
			$zjm = strtoupper($zjm);

			$fmt = "INSERT INTO %s(info, info_id, bank, branch, province, city, zone, type, name, zjm, code, createtime, modifytime)";
			$fmt .= " VALUES('1', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
			$sql = sprintf($fmt, $this->tbl, $info_id, $bank, $branch, $province, $city, $zone, $type, $name, $zjm, $code);
			echo $sql . "\n";
			$db->query($sql);
			
		}
		fclose($fd);

		echo "\n" . $_REQUEST['do'] . "\n";

		logdate($controller, $request);
		return true;
	}
	
	function zh2($controller, $request) {
		$db = newdb($this->dsn_w);

		$file = "/home/sunxy/zh2.txt";
		$fd = fopen($file, "r");
		while (!feof($fd)) {
			$buf = trim(fgets($fd));
			// echo $buf;

			if (strlen($buf) < 5 || preg_match("/^(#|-)/i", $buf)) {
				continue;
			}
			
			$a = preg_split("/;/", $buf);

			// print_r($a);

			$bank = '招商银行';
			$branch = '深圳分行';
			
			$province = '广东';
			$city = '深圳';			
			$zone = '0755';
			$type = '1';
			
			$name = trim($a[1]);			
			$code = trim($a[2]);
			$branch = trim($a[3]);
			
			$info_id = $this->_emp_name($name);
			
			$zjm = getPinyin($name);
			$zjm = strtoupper($zjm);

			$fmt = "INSERT INTO %s(info, info_id, branch, province, city, zone, type, name, zjm, code, createtime, modifytime)";
			$fmt .= " VALUES('1', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
			$sql = sprintf($fmt, $this->tbl, $info_id, $branch, $province, $city, $zone, $type, $name, $zjm, $code);
			echo $sql . "\n";
			$db->query($sql);
			
		}
		fclose($fd);

		echo "\n" . $_REQUEST['do'] . "\n";

		logdate($controller, $request);
		return true;
	}
	
	function _my_info($controller, $info, $id) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE info = '%s' AND info_id = '%s' ORDER BY id DESC";
		$sql = sprintf($fmt, $this->tbl, $info, $id);
		debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row['code'] = $this->_format($row['code'], 4, ' ');
			
			$row = $this->_view_t($row);
			
			$ret[] = $row;
		}

		$controller->assign_by_ref('my', $ret);
	}
	
	function emp($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		
		$this->_my_info($controller, '1', $id);

		$controller->display($request, 'finance/bank/emp');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function empPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$emp = $id;
		
		$bank = $request['bank'];
		$branch = $request['branch'];
		$bankcode = $this->_input($request['bankcode']);
		
		$province = $request['province'];
		$city = $request['city'];
		
		$type = $request['type'];
		$zone = $request['zone'];
		
		$name = trim($request['name']);
		$code = trim($request['code']);
		$code = str_replace(" ", "", $code);
		
		$content = addslashes($request['content']);
		
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);

		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE name = '%s' AND code = '%s'";
		$sql = sprintf($fmt, $this->tbl, $name, $code);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			$key = $this->tbl . "_id_" . $row['id'];
			clean($key);
			
			$fmt = "UPDATE %s SET info = '1', info_id = '%s' WHERE id = '%s'";
			$sql = sprintf($this->tbl, $id, $row['id']);
		} else {
			$fmt = "INSERT INTO %s(office, dept, info, info_id, bank, branch, bankcode, province, city, zone, type, name, code, content, oper, createtime, modifytime)";
			$fmt .= " VALUES('%s', '%s', '1', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
			$sql = sprintf($fmt, $this->tbl, $office, $dept, $id, $bank, $branch, $bankcode, $province, $city, $zone, $type, $name, $code, $content, $_SESSION['id']);
		}
		debug($sql);
		$db->query($sql);
		
		return true;
	}
	
	function supplier($controller, $request) {
		$this->pretreat($controller);
		
		$id = $request['id'];
		
		$this->_my_info($controller, '2', $id);
		
		$controller->display($request, 'finance/bank/supplier');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function supplierPost($controller, $request) {
		$this->pretreat($controller);
		
		$this->_insert($request, '2');
		
		return true;
	}
	
	function _insert($request, $info) {

		$id = $request['id'];
		
		$category = $request['category'];
		$bank = trim($request['bank']);
		$branch = trim($request['branch']);
		$bankcode = $this->_input($request['bankcode']);
		
		$province = $request['province'];
		$city = $request['city'];
		
		$type = $request['type'];
		$zone = $request['zone'];
		
		$swift = trim($request['swift']);
		$name = trim($request['name']);
		$code = trim($request['code']);
		$code = str_replace(" ", "", $code);
		
		$content = addslashes($request['content']);
		
		$oper = $_SESSION['id'];
		$emp = $oper;
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE name = '%s' AND code = '%s'";
		$sql = sprintf($fmt, $this->tbl, $name, $code);
		debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			$key = $this->tbl . "_id_" . $row['id'];
			clean($key);
				
			$fmt = "UPDATE %s SET info = '%s', info_id = '%s' WHERE id = '%s'";
			$sql = sprintf($this->tbl, $info, $id, $row['id']);
		} else {
			$fmt = "INSERT INTO %s(office, dept, info, info_id, category, bank, branch, bankcode, swift, province, city, zone, type, name, code, content, oper, createtime, modifytime)";
			$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
			$sql = sprintf($fmt, $this->tbl, $office, $dept, $info, $id, $category, $bank, $branch, $bankcode, $swift, $province, $city, $zone, $type, $name, $code, $content, $oper);
		}
		debug($sql);
		$db->query($sql);
	}
	
	function customer($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
	
		$this->_my_info($controller, '3', $id);
	
		$controller->display($request, 'finance/bank/customer');
		return true;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function customerPost($controller, $request) {
		$this->pretreat($controller);
	
		$this->_insert($request, '3');
		
		return true;
	}
	
	function _update($info, $info_id, $json) {
		if (empty($info_id) || !is_numeric($info_id)) {
			return false;
		}
		
		$branch = trim($json->bank);
		$name = trim($json->name);
		$code = trim($json->code);
		$content = ($json->content);		
		if (empty($branch) || empty($name) || empty($code)) {
			return false;
		}
		
		$code = str_replace(" ", "", $code);
		
		$type = 1;
		if (strlen($name) > 20) {
			$type = 2;
		}
		
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE name = '%s' AND code = '%s'";
		$sql = sprintf($fmt, $this->tbl, $name, $code);
		debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			$key = $this->tbl . "_id_" . $row['id'];
			clean($key);
			
			$fmt = "UPDATE %s SET info = '%s', info_id = '%s', content = concat(content, '%s'), modifytime = now() WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $info, $info_id, $content, $row['id']);			
			// $db->query($sql);
		} else {
			$fmt = "INSERT INTO %s(info, info_id, branch, type, name, code, content, oper, createtime, modifytime)";
			$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
			$sql = sprintf($fmt, $this->tbl, $info, $info_id, $branch, $type, $name, $code, $content, $_SESSION['id']);
			$db->query($sql);
		}
		debug($sql);
	}
	
	function z($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE bank = '中国工商银行'";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$branch = preg_replace("/深圳|深圳市/", "深圳市", $row['branch']);
		
			$fmt = "UPDATE %s SET branch = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $branch, $row['id']);
			echo $sql . "\n";
			// $db->query($sql);
			$this->_query($sql);
		}

		logdate($controller, $request);
		return true;
	}
	
	function _query($sql) {
		$db = newdb($this->dsn_w);
		$db->query($sql);
	}
		
	function zjm($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$zjm = getPinyin($row['name']);
			$zjm = strtoupper($zjm);

			$fmt = "UPDATE %s SET zjm = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $zjm, $row['id']);
			// debug($sql);
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}
	
	function ajax_id($controller, $request) {
		$q = $request['q'];
		$data = $this->id($this->tbl, $q);		
		$data['code'] = $this->_format($data['code'], 4, ' ');
		
		echo json_encode($data);
	}
	
	function _bank_primary($info, $info_id) {
		if (empty($info) || !is_numeric($info_id)) {
			return false;
		}
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE info = '%s' AND info_id = '%s' AND status = '1' ORDER BY orderby";
		$sql = sprintf($fmt, $this->tbl, $info, $info_id);
		// debug($sql);
		$result = $db->query($sql);
		
		return $db->fetch_array($result);
	}
	
	function _bank_salary($emp, $tec) {
		if (!is_numeric($emp)) {
			return false;
		}
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE info = '1' AND info_id = '%s' AND status = '1' AND tec = '%s' ORDER BY orderby";
		$sql = sprintf($fmt, $this->tbl, $emp, $tec);
		// debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			return $row;
		}
		
		$fmt = "SELECT * FROM %s WHERE info = '1' AND info_id = '%s' AND status = '1' AND tec = '3' ORDER BY orderby";
		$sql = sprintf($fmt, $this->tbl, $emp);
		// debug($sql);
		$result = $db->query($sql);
				
		return $db->fetch_array($result);
	}
	
	function _bank($info, $info_id, $payment_x) {
		if (empty($info_id) || !is_numeric($info_id)) {
			return false;
		}
		
		$payment_x = json_decode($payment_x);
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE info = '%s' AND info_id = '%s' AND status = '1' ORDER BY orderby";
		$sql = sprintf($fmt, $this->tbl, $info, $info_id);
		// debug($sql);
		$result = $db->query($sql);		
		while($row = $db->fetch_array($result)) {
			$code = str_replace(" ", "", $payment_x->code);
			if ($code == $row['code']) {
				$row['name'] = $payment_x->name;
				
				return $row;
			}
		}
		
		return false;
	}
	
	function _ajax_bank($info, $info_id) {
		if (empty($info_id) || !is_numeric($info_id)) {
			return array();
		}
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE info = '%s' AND info_id = '%s' AND status = '1' ORDER BY orderby";
		$sql = sprintf($fmt, $this->tbl, $info, $info_id);
		// debug($sql);
		$result = $db->query($sql);
		$ret = array(array('id'=>'-', 'name'=>'请选择'));
		while ($row = $db->fetch_array($result)) {			
			$row = $this->_view(null, $row);
			
			$ret[] = $row;
		}
				
		return $ret;
	}
	
	function _name_code($name, $code) {
		if (empty($name) || empty($code)) {
			return false;
		}
		
		$code = str_replace(" ", "", $code);
	
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE name = '%s' AND code = '%s'";
		$sql = sprintf($fmt, $this->tbl, $name, $code);
		debug($sql);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			return true;
		}
		
		return false;
	}
	
	function ajax_zjm($controller, $request) {

		$data = $this->_ajax_zjm($request);
		echo json_encode($data);
	}

	function _ajax_zjm($request) {
		$q = $request['q'];
		$n = $request['maxRows'];
		if (empty($n)) {
			$n = 20;
		}
		
		if (preg_match("/^(\d+)(.+)/", $q, $m)) {
			$status = $m[1];
			$zjm = $m[2];
		} else {
			$status = 1;
			$zjm = $q;
		}
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE status = '%s' AND zjm LIKE '%s' ORDER BY id DESC LIMIT %s";
		$sql = sprintf($fmt, $this->tbl, $status, $q . "%", $n);
		debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {

			$ret[] = $row;
		}

		return $ret;
	}
	
	function icbc($controller, $request) {
		include(APP_LIB . '/simple_html_dom.php');
		
		$url = 'http://www.boc.cn/sourcedb/whpj/';
		$url = "/home/sunxy/Desktop/icbc/AreaCodesConf.xml";
		$url = "/home/sunxy/Desktop/icbc/BPAccountList_Sort.xml";
		$html = file_get_html($url);		
		// echo $html . "\n";
	
		$dom = new DOMDocument();
		$dom->load('/home/sunxy/Desktop/icbc/BPAccountList_Sort.xml');
		$book = $dom->documentElement;
		foreach($book->childNodes as $node) {
			print_r($node);
		}
	
		
		//print_r($ret);

		logdate($controller, $request);
		return true;
	}
	
	function cccc($controller, $request) {
		$p = new myParser();
		$result = $p->setInputFile('/home/sunxy/Desktop/icbc/BPAccountList_Sort.xml');
		$result = $p->parse();
		
		logdate($controller, $request);
		return true;
	}
	
	function merge($info, $from, $to) {
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET info_id = '%s' WHERE info = '%s' AND info_id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $to, $info, $from);
		debug($sql);
		$db->query($sql);
	}
}
?>